/**
 * 
 */
package cl.kaisanetwork.kaisaportal.page;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.click.Page;
import org.apache.click.util.Bindable;
import org.springframework.stereotype.Component;

import cl.kaisanetwork.kaisaportal.domain.entities.Usuario;
import cl.kaisanetwork.kaisaportal.service.UsuarioService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;

/**
 * @author Tomás Tapia Silva
 * 
 */

@Component
public class BorderPage extends Page {

	private static final long serialVersionUID = 1L;

	public static String HOME = "home";
	public static String SERVICES = "services";
	public static String SUPPORT = "support";
	public static String CONTACT = "contact";
	public static String PROJECT = "project";
	public static String ABOUT = "about";
	public static String WIKI = "wiki";
	public static String CONTROLPANEL = "controlpanel";

	@Bindable
	protected String navMenuSelected = BorderPage.HOME;

	@Bindable
	protected String title = "";
	
	@Bindable
	protected String textTopBox = "";

	@Resource
	protected UserService userService;
	
	@Resource
	protected UsuarioService usuarioService;

	public User googleUser;
	
	@Bindable
	public Usuario currentUser;

	@Override
	public void onInit() {
		super.onInit();
		this.googleUser = userService.getCurrentUser();
		if (googleUser == null) {
			addModel("isUserLogin", false);
			addModel("urlLogin", this.userService.createLoginURL("/accounts/login.htm", "kaisanetwork.net"));
		} else {
			currentUser = this.usuarioService.findUsuarioByGoogleId(googleUser.getUserId());
			usuarioService.makeRequest(currentUser.getId(), new Date());
			addModel("isUserLogin", true);
			addModel("googleUser", googleUser);
			addModel("urlLogout", this.userService.createLogoutURL("/home/home.htm", "kaisanetwork.net"));
		}
	}

	@Override
	public String getTemplate() {
		return "/templates/border-template.htm";
	}
	
	@Override
	public boolean onSecurityCheck() {
		if(googleUser == null)
			return true;
		else
			if(currentUser == null) {
				return false;
			} else
				return true;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTextTopBox() {
		return textTopBox;
	}

	public void setTextTopBox(String textTopBox) {
		this.textTopBox = textTopBox;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getNavMenuSelected() {
		return navMenuSelected;
	}

	public void setNavMenuSelected(String navMenuSelected) {
		this.navMenuSelected = navMenuSelected;
	}

	public UsuarioService getUsuarioService() {
		return usuarioService;
	}

	public void setUsuarioService(UsuarioService usuarioService) {
		this.usuarioService = usuarioService;
	}
}
